package cn.lds.j367.dsbank.service.impl;

import cn.lds.j367.dsbank.common.condition.CustomerCondition;
import cn.lds.j367.dsbank.common.constant.ResultConstant;
import cn.lds.j367.dsbank.common.dto.ResultDTO;
import cn.lds.j367.dsbank.common.utils.Pager;
import cn.lds.j367.dsbank.entity.Customer;
import cn.lds.j367.dsbank.mapper.CustomerMapper;
import cn.lds.j367.dsbank.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO<List<Customer>> selectCustomerByCustomerCondition(CustomerCondition condition, Pager pager) {

        List<Customer> customers = customerMapper.selectCustomerByCustomerCondition(condition, pager);

        if (CollectionUtils.isEmpty(customers)){
            return ResultDTO.buildError(ResultConstant.queryMsg.QUERY_ERROR);
        }


        return ResultDTO.buildSuccess(customers);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO selectCustomerCount(CustomerCondition condition) {
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO<Customer> selectCustomerByCustomerId(Integer customerId) {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO addCustomer(Customer customer) {

        int i = customerMapper.addCustomer(customer);

        if (i<= 0){
            return ResultDTO.buildError();
        }

        return ResultDTO.buildSuccess();
    }
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO updateCustomer(Customer customer) {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO deleteCustomer(Integer customerId) {
        return null;
    }
}
